package com.ujoku.UrlHelper;

import com.ujoku.request.UrlHelperForm;
import com.ujoku.view.domain.UrlHelperView;

import java.util.HashMap;

/**
 * Created by suggestion on 2015/5/15.
 */
public class UrlSearchHelper extends UrlHelperBase {
    @Override
    public UrlHelperView getView(UrlHelperForm form) {
        this.view = new UrlHelperView();

        view.setAction("search");

        int index = form.getUrl().indexOf("?");
        if(index == -1)
            return view;

        view.setPairs(new HashMap<>());
        String query = form.getUrl().substring(index + 1);
        String[] pairs = query.split("&");
        for (String pair : pairs) {
            int idx = pair.indexOf("=");
            if(idx == -1)
                continue;
            String key = pair.substring(0, idx);
            String value = pair.substring(idx + 1);
            if(key.equalsIgnoreCase("q"))
                key = "freeText";

            view.getPairs().put(key, value);
            //query_pairs.put(URLDecoder.decode(pair.substring(0, idx), "UTF-8"), URLDecoder.decode(pair.substring(idx + 1), "UTF-8"));
        }

        return view;
    }

}
